<?php   
$GLOBALS['THRIFT_ROOT'] = '/usr/local/thrift/thrift-0.9.0/lib/php/lib';  
/* Remember these two files? */  
require_once 'Types.php';  
require_once 'KYPService.php';  

/* Dependencies. In the proper order. */  
require_once $GLOBALS['THRIFT_ROOT'].'/Thrift/Transport/TTransport.php';  
require_once $GLOBALS['THRIFT_ROOT'].'/Thrift/Transport/TSocket.php';  
require_once $GLOBALS['THRIFT_ROOT'].'/Thrift/Protocol/TProtocol.php';  
require_once $GLOBALS['THRIFT_ROOT'].'/Thrift/Protocol/TBinaryProtocol.php';  
require_once $GLOBALS['THRIFT_ROOT'].'/Thrift/Transport/TBufferedTransport.php';  
require_once $GLOBALS['THRIFT_ROOT'].'/Thrift/Type/TMessageType.php';  
require_once $GLOBALS['THRIFT_ROOT'].'/Thrift/Factory/TStringFuncFactory.php';  
require_once $GLOBALS['THRIFT_ROOT'].'/Thrift/StringFunc/TStringFunc.php';  
require_once $GLOBALS['THRIFT_ROOT'].'/Thrift/StringFunc/Core.php';  
require_once $GLOBALS['THRIFT_ROOT'].'/Thrift/Type/TType.php';  
use Thrift\Protocol\TBinaryProtocol;  
use Thrift\Transport\TSocket;  
use Thrift\Transport\TSocketPool;  
use Thrift\Transport\TFramedTransport;  
use Thrift\Transport\TBufferedTransport;  
$host = 'localhost';  
$port = 9898;  
$socket = new Thrift\Transport\TSocket($host, $port);  
$transport = new TBufferedTransport($socket);  
$protocol = new TBinaryProtocol($transport);  
// Create a calculator client  
$client = new \KYPThrift\KYPServiceClient($protocol);  
$transport->open();  

//print_r("5");
////////////////////////////////////////////////////////////////////////////////////////////

$Temp = array();
$result = $client -> GetData("KYP");  
$Temp = (array)$result;

//unset($Temp["columncount"]);
//unset($Temp["rowcount"]);
//unset($Temp["columnkey"]);

$TempTable = array();
//var_dump($Temp);

$iColCount;
$iRowCount;
$arrayColumnNames;
$arrayDatas;

$iColCount = (int)$Temp["columncount"];
$iRowCount = (int)$Temp["rowcount"];
$arrayColumnNames = (array)$Temp["columnkey"];
$arrayDatas = (array)$Temp["datatable"];
$arrayDataType = (array)$Temp["datattype"];

$arrayData = array();

//echo($iColCount);
//echo($iRowCount);

//var_dump($row3);
//var_dump($row4);


for($iRow=0; $iRow <= $iRowCount; $iRow++){
	
	if($iRow == 0){
		for($iCol=0; $iCol < $iColCount; $iCol++){
			$arrayData[0][$iCol] = $arrayColumnNames[$iCol];
		}
	}else{
		for($iCol=0; $iCol < $iColCount; $iCol++){
			$arrayData[$iRow][$iCol] = ($arrayDataType[$iCol])$arrayDatas[$arrayColumnNames[$iCol]][$iRow - 1];
		}
	}
}

header('content-type: application/json');
$Temp = json_encode($arrayData );
echo $Temp;
?>